﻿using System.Collections.Generic;
using Microsoft.SqlServer.Dts.Pipeline.Wrapper;
using NazTek.Documenter.Ssis.Control.Tasks.PipeSubComponent;

namespace NazTek.Documenter.Ssis.Control.Tasks
{
    internal enum AccessMode
    {
        SqlCommand = 2,
        OpenRowSet = 3
    }
    internal sealed class PipeSubComponentList : List<Base>
    {
        internal SsisPipe Parent { get; set; }
        internal PipeSubComponentList(IEnumerable<IDTSComponentMetaData100> subComponents, SsisPipe parent)
        {
            Parent = parent;
            AddSubComponents(subComponents);
        }

        private void AddSubComponents(IEnumerable<IDTSComponentMetaData100> subComponents)
        {
            foreach (var sc in subComponents)
            {
                switch (sc.ContactInfo.Split(";".ToCharArray())[0])
                {
                    case ("Extracts data from a relational database by using a .NET provider."):
                    case ("OLE DB Command"):
                        Add(new DataReaderSource(Parent, sc));
                        break;
                    case ("OLE DB Source"):
                        if (Utility.GetAccessMode(sc) == AccessMode.SqlCommand)
                            Add(new DataReaderSource(Parent, sc));
                        else
                            Add(new OleDbSource(Parent, sc));
                        break;
                    case ("Data Conversion"):
                        Add(new DataConversion(sc));
                        break;
                    case ("Derived Column"):
                        Add(new DerivedColumn(sc));
                        break;
                    case ("Lookup"):
                        Add(new Lookup(Parent, sc));
                        break;
                    case ("Conditional Split"):
                        Add(new ConditionalSplit(sc));
                        break;
                    case ("Union All"):
                        Add(new UnionAll(Parent, sc));
                        break;
                    case ("OLE DB Destination"):
                        Add(new OleDbDestination(Parent, sc));
                        break;
                    default:
                        Add(new UnsupportedSub(sc));
                        break;
                }
            }
        }
    }

    internal sealed class CopyOfPipeSubComponentList : List<Base>
    {
        internal SsisPipe Parent { get; set; }
        internal CopyOfPipeSubComponentList(IEnumerable<IDTSComponentMetaData100> subComponents, SsisPipe parent)
        {
            Parent = parent;
            AddSubComponents(subComponents);
        }

        private void AddSubComponents(IEnumerable<IDTSComponentMetaData100> subComponents)
        {
            foreach (var sc in subComponents)
            {
                switch (sc.ContactInfo.Split(";".ToCharArray())[0])
                {
                    case ("Extracts data from a relational database by using a .NET provider."):
                    case ("OLE DB Command"):
                        Add(new DataReaderSource(Parent, sc));
                        break;
                    case ("OLE DB Source"):
                        if (Utility.GetAccessMode(sc) == AccessMode.SqlCommand)
                            Add(new DataReaderSource(Parent, sc));
                        else
                            Add(new OleDbSource(Parent, sc));
                        break;
                    case ("Data Conversion"):
                        Add(new DataConversion(sc));
                        break;
                    case ("Derived Column"):
                        Add(new DerivedColumn(sc));
                        break;
                    case ("Lookup"):
                        Add(new Lookup(Parent, sc));
                        break;
                    case ("Conditional Split"):
                        Add(new ConditionalSplit(sc));
                        break;
                    case ("Union All"):
                        Add(new UnionAll(Parent, sc));
                        break;
                    case ("OLE DB Destination"):
                        Add(new OleDbDestination(Parent, sc));
                        break;
                    default:
                        Add(new UnsupportedSub(sc));
                        break;
                }
            }
        }
    }
}